jQuery(function($) {

	//endpoints = ["http://catalogus-professorum.org/sparql","http://dbpedia.org/sparql"];
	endpoints = ["https://pcai042.informatik.uni-leipzig.de/~swp10-7/ontowiki/sparql"]; //The used SPARQL endpoint
	limit = 10;							//the limit of resultelements
	inputChars = 3;						//number of inputted characters after search begins
	shownFacets = 6;					//limit of shown facets
	$('#input').alida({				//initializing alida
		'endpoints': endpoints,
		'limit': limit,
		'inputChars': inputChars,
		'shownFacets': shownFacets,
	});
	$input = $('#input');				//alias for the input field
	$input.ownerDocument = document;
	testInput = "max";
	
	
	//function to test if the popup was hidden
	function testStyleNone(){
		if(document.getElementById('popup').style.display == "none"){
			window.clearInterval(testHide);
			start();
		}
	};
	
	
	//funtion to test if the popup is shown again
	function testStyleBlock(){
		if(document.getElementById('popup').style.display == "block"){
			window.clearInterval(testShow);
			start();
		}
	};
	
	
	//function to test whether the result list is no longer empty
	function resCont(){
		if($('#results').find('li').html() != null){
			window.clearInterval(testInt);
			start();
		}
	};
	
	
	//function to test whether the facets list is no longer empty
	function resFacets(){
		if($('#facets').find('li').html() != null){
			window.clearInterval(testFacets);
			start();
		}
	};
	
/*--Module 1---------------------------------------------------------------------------------------------*/
				module("Popup");
				
				test("Test alida popup is not shown when #input is not yet focused", function() {
					ok( $(".alida").css('display') == "none", "Popup is hidden" );
					$input.focus();					//the initial interaction with the input field is simulated
					$("#popup").parent().mouseover();
					$("#alida").click();
					stop();							//stop the tests and then wait till the popup is shown, since it is set to "slow"
					testShow = setInterval(testStyleBlock, 200);
				});
				
				test("Test alida popup is shown when #input is focused", function() {
					ok( $(".alida").css('display') == "block", "Popup is shown"  );
					$("#popup").parent().mouseout();		//clicking out of the input field is simulated
					$("body").click();
					stop();							//stop the tests and wait, till the popup is hidden
					testHide = setInterval(testStyleNone, 200);
				});
				
				test("Test alida popup is hidden when clicked outside #alida", function(){
					ok( $(".alida").css('display') == "none", "Popup was hidden" );
					stop();
					$("#popup").parent().mouseover();
					$("#alida").click();
					testShow = setInterval(testStyleBlock, 200);
				});
				
				test("Test alida popup is shown again when clicked into the alida", function() {
					ok( $(".alida").css('display') == "block", "Popup is shown"  );
				});
				
				
				
/*--Module 2--------------------------------------------------------------------------------------------*/
				module("Initial state")
				
				test("Test the endpoint identifiers equal the passed parameters", function(){
					for(var i=0; i<endpoints.length; i++){				//for every passed endpoint, look if it can be found in the popup's endpoints list
						check = false;
						$("#endpoints").find("a").each(function(){
							if($(this).text()==endpoints[i].identifier){
								check = true;
							}
						});
						ok(check, "Endpoint-identifier #" + (i+1) + " equals passed parameter");
					}
				});	
				
				test("Test #results is empty when nothing was entered into #input", function(){
					equals($('#results').children().length, 0, "Result list intially empty. Children");
				});
				
				
/*--Module 3-------------------------------------------------------------------------------------------*/
				module("Text input");
				
				test("no search results if input.length less than " + inputChars, function(){
					var inputDummy = "";
					for(var i=0; (i < inputChars) && (i < testInput.length-1); i++){
						inputDummy += testInput[i];
						$input.val(inputDummy);					//simulating the input of letters
						$input.keypress();
						equals($('#results').children().length, 0, "Result list still empty. Input=\""+$input.val()+"\"; List-Children");
					}
					
					$input.val(testInput);						//adding the entire test input string into the input field
					$input.keypress();
					stop();
					testInt = setInterval(resCont, 200);		//wait for the sparql query to be sent
				});
				
				test("Test search results were added to #results", function(){
					ok($('#results').children().length != 0, "The result list is no longer empty. input string: " + testInput);
				});
				
				test("Test results number is less or equal to the number specified in parameters", function(){
					ok( $(".resultelement").length <= limit, 
						"The number of listed results is within the specified range. Limit: " + limit + ", results: " + $(".resultelement").length);
					stop();
					testFacets = setInterval(resFacets, 200);	//wait for facets to be added
				});
				
				
/*--Module 4-------------------------------------------------------------------------------------------*/
				module("Facets");
				
				test("Test facets shown", function(){
					ok( $("#facets").children().length != 0, "Facets were added to the popup.");
				});
				
				test("Test facets number is less or equal to the number specified in parameters", function(){
					ok( $("#facets").children().length <= shownFacets, 
						"The number of shown facets is within specified range. Limit: " + shownFacets + ", facets: " + $("#facets").children().length);
				});
				
				
/*--Module 5-------------------------------------------------------------------------------------------*/
				module("Click a result");
				
				test("Clicking a result changes input value", function(){
					$(".resultelement:first").click();			//clicking the first result element in the list
					ok( $(".resultelementObject:first").text() != testInput, "The first resultelement is different from the input string.");
					ok( $input.val() != testInput, "The input value was changed.");
					equals( $input.val(), $(".resultelementObject:first").text(), "The new value of the input box is equal to the first resultelement.");
					$('form').children().css('display','none');		//hiding the css
				});
});
